import machine
import json
import SCBoard_app
import mars_rover_trail

speed = 1
# 设置火星车引脚
rover_trail = mars_rover_trail.mars_rover_trail(22,23)


def app_con_data_tcp(msg):
    print(msg)
    global my_tcp_client
    global speed
    mymsg=json.loads(msg)
    if "mc_wheel" == mymsg['type']:
        speed = int((int(mymsg['speed']) * 40))
        print(speed)
        if "mcbut_N" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,speed)
                rover_trail.carB(1,speed)
                rover_trail.carC(1,speed)
                rover_trail.carD(1,speed)

            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_S" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(0,speed)
                rover_trail.carB(0,speed)
                rover_trail.carC(0,speed)
                rover_trail.carD(0,speed)
                
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_W" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(0,speed)
                rover_trail.carB(0,speed)
                rover_trail.carC(1,speed)
                rover_trail.carD(1,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_E" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,speed)
                rover_trail.carB(1,speed)
                rover_trail.carC(0,speed)
                rover_trail.carD(0,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_WW" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(0,speed)
                rover_trail.carB(1,speed)
                rover_trail.carC(1,speed)
                rover_trail.carD(0,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_EE" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,speed)
                rover_trail.carB(0,speed)
                rover_trail.carC(0,speed)
                rover_trail.carD(1,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_WN" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,0)
                rover_trail.carB(1,speed)
                rover_trail.carC(1,speed)
                rover_trail.carD(1,0)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_ES" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,1)
                rover_trail.carB(0,speed)
                rover_trail.carC(0,speed)
                rover_trail.carD(1,1)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_EN" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(1,speed)
                rover_trail.carB(1,1)
                rover_trail.carC(1,1)
                rover_trail.carD(1,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
        if "mcbut_WS" == mymsg['direction']:
            if "down" == mymsg['value']:
                rover_trail.carA(0,speed)
                rover_trail.carB(1,1)
                rover_trail.carC(1,1)
                rover_trail.carD(0,speed)
            else:
                rover_trail.carA(1,0)
                rover_trail.carB(1,0)
                rover_trail.carC(1,0)
                rover_trail.carD(1,0)
    if "mc_wheel_jixiebi" == mymsg['type']:
        if "0" == mymsg['num']:
            rover_trail.carservo.servo(0,int(mymsg["value"]))
        if "1" == mymsg['num']:
            rover_trail.carservo.servo(1,int(mymsg["value"]))
        if "2" == mymsg['num']:
            rover_trail.carservo.servo(2,int(mymsg["value"]))


# 设置火星车连接的wifi和设备地址
my_tcp_client = SCBoard_app.SCBoard_app_tcp("AiBoard","Ai123456","zksp2017",6688)
my_tcp_client.app_con_data_tcp(app_con_data_tcp)


